﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Xml.Linq;

namespace Lightray.Hattrick.ChppLib.EntityTypes
{
    [ComVisible(true)]
    public class Team : EntityBase
    {
        #region Properties
        public string Name { get; private set; }
        public string ShortName { get; private set; }
        public string Arena { get; private set; }
        public bool IsBot { get; private set; }
        public bool StillInCup { get; private set; }
        public Manager Manager { get; private set; }                
        public int FriendlyTeamID { get; private set; }
        public bool HasFriendlyBooked { get { return FriendlyTeamID > 0; } }
        public Uri LogoUri { get; private set; }
        public int ConsecutiveStreak { get; private set; }
        public int WinningStreak { get; private set; }
        public int Rank { get; private set; }
        public IEnumerable<Trophy> TrophyList { get; private set; }        
        public EntityCollection<Player> Players { get; internal set; }
        public LeagueLevelUnit League { get; internal set; }
        public TeamLeagueStats LeagueStats { get; internal set; }
        public LeagueCountry Country { get; internal set; }
        public Region Region { get; internal set; }
        public Economy Economy { get; internal set; }
        public EntityCollection<Match> Matches { get; internal set; }

        internal int LeagueID { get; private set; }
        internal int RegionID { get; private set; }
        internal int LeagueLevelUnitID { get; private set; }
                
        #endregion

        protected Team() { }            

        public static Team FromXml(XDocument teamXml)
        {
            HattrickXmlReader reader = new HattrickXmlReader(teamXml);

            return new Team()
            {
                Manager = Manager.FromTeamXml(reader.ReadElement("User")),
                // TODO: change to consts
                ID = reader.ReadNumber("TeamID"),
                DataTime = reader.ReadDate("FetchedDate"),
                Name = reader.ReadString("TeamName"),
                ShortName = reader.ReadString("ShortTeamName"),
                Arena = reader.ReadString("ArenaName"),
                IsBot = reader.ReadBoolean("IsBot"),
                Rank = reader.ReadNumber("TeamRank"),
                StillInCup = reader.ReadBoolean("StillInCup"),
                FriendlyTeamID = reader.ReadNumber("FriendlyTeamID"),
                ConsecutiveStreak = reader.ReadNumber("NumberOfVictories"),
                WinningStreak = reader.ReadNumber("NumberOfUndefeated"),
                LogoUri = reader.ReadUri("LogoURL"),
                TrophyList = readTrophyList(teamXml.Descendants("Trophy")),

                LeagueID = reader.ReadNumber("LeagueID"),
                RegionID = reader.ReadNumber("RegionID"),
                LeagueLevelUnitID = reader.ReadNumberOrDefault("LeagueLevelUnitID", -2),
            };
        }

        private static IEnumerable<Trophy> readTrophyList(IEnumerable<XElement> trophies)
        {
            List<Trophy> trophyList = new List<Trophy>();
            foreach (var trophy in trophies)
                trophyList.Add(Trophy.FromTeamXml(trophy));
            return trophyList.AsEnumerable();
        }
        
        public override string ToString()
        {
            return Name;
        }                           
    }
}

  
  